<!-- dialog -->
<template>
  <el-dialog title="新增工单" :visible="dialogVisible" @close="close">
    <el-form ref="addRef" :model="form" :rules="formRules">
      <el-form-item label="设备编号" :label-width="formLabelWidth" prop="innerCode">
        <el-input
          v-model="form.innerCode"
          autocomplete="off"
          placeholder="请输入"
          maxlength="15"
          show-word-limit
          @blur="blurFn"
        />
      </el-form-item>
      <el-form-item label="工单类型" :label-width="formLabelWidth" prop="productType">
        <el-select
          v-model="form.productType"
          placeholder="请选择"
          style="width: 100%"
        >
          <el-option
            v-for="item in productTypeList"
            :key="item.typeId"
            :label="item.typeName"
            :value="item.typeName"
          />
        </el-select>
      </el-form-item>
      <el-form-item v-show="!params.isRepair" label="补货数量" :label-width="formLabelWidth">
        <el-button type="text" icon="el-icon-edit-outline">补货清单</el-button>
      </el-form-item>
      <el-form-item :label="title" :label-width="formLabelWidth" prop="assignorId">
        <el-select
          v-model="form.assignorId"
          placeholder="请选择"
          style="width: 100%"
        >
          <el-option
            v-for="item in userList"
            :key="item.userId"
            :label="item.userName"
            :value="item.userName"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="备注" :label-width="formLabelWidth" prop="desc">
        <el-input
          v-model="form.desc"
          type="textarea"
          autocomplete="off"
          placeholder="请输入备注（不超过40字）"
          maxlength="40"
          :autosize="{ minRows: 3, maxRows: 6 }"
          show-word-limit
        />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="close">取 消</el-button>
      <el-button
        type="primary"
        @click="submit"
      >确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { taskTypeList, createTask, userRepairerList } from '@/api'
export default {
  props: {
    dialogVisible: {
      type: Boolean,
      default: false
    },
    params: {
      type: Object,
      default: null
    }
  },
  data() {
    return {
      dialogFormVisible: false,
      form: {
        innerCode: '',
        productType: '',
        assignorId: '',
        desc: ''
      },
      formLabelWidth: '120px',
      productTypeList: [],
      userList: [],
      formRules: {
        innerCode: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ],
        productType: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ],
        assignorId: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ],
        desc: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {
    title() {
      return this.params.isRepair ? '运维人员' : '运营人员'
    }
  },
  async created() {
    const res = await taskTypeList()
    if (this.params.isRepair) {
      console.log(res)
      this.productTypeList = res.data.filter(item => item.type === 1)
    } else {
      this.productTypeList = res.data.filter(item => item.type === 2)
    }
  },
  methods: {
    close() {
      this.$emit('close')
      this.$refs.addRef.resetFields()
    },
    // 失去input焦点触发事件
    async blurFn() {
      if (this.params.isRepair) {
        const res = await userRepairerList(this.form.innerCode)
        console.log(res)
        this.userList = res.data
        console.log(this.userList)
      }
    },
    submit() {
      this.$refs.addRef.validate(async(valid) => {
        if (!valid) return
        // assignorId: +this.userList.filter(item => item.userName === this.form.assignorId),
        await createTask({
          ...this.form,
          productType: +this.productTypeList.filter(item => item.typeName === this.form.productType)[0].typeId,
          assignorId: +this.userList.filter(item => item.userName === this.form.assignorId)[0].userId,
          createType: 1,
          userId: 1
        })
        // console.log(this.form)
        this.$message.success('新增员工成功')
        this.close()
        this.$emit('add')
      })
    }
  }
}
</script>
<style lang='scss' scoped>
::v-deep .el-dialog__header {
  background: #fff;
  .el-dialog__title {
    font-weight: 700;
    font-size: 16px;
    color: #333;
  }
}
::v-deep .el-icon-close:before {
    /* color: #fff; */
    color: #909399;
  }
</style>
